package com.projectsierra.springappengine;
import javax.servlet.http.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.walczak.piotr.Decrypt;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Logger;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import user.UserData;

public class RegisterServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(RegisterServlet.class.getName());
	private static int id=1;
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
             
    
            

      
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    		throws IOException{
    	
    	UserData userBean = (UserData) request.getSession().getAttribute("mybean");
      
        log.info(userBean.getEmail() + "DDDDDDDDD");
    	

    	Key guestbookKey = KeyFactory.createKey("Uzytkownik", "Uzytkownik");
    	Entity uzytkownik = new Entity("Uzytkownik", guestbookKey);
    	uzytkownik.setProperty("id", id);
        uzytkownik.setProperty("first_name", userBean.getFirst_name());
        uzytkownik.setProperty("second_name", userBean.getSecond_name());
        uzytkownik.setProperty("city", userBean.getCity());
        uzytkownik.setProperty("birth_date", userBean.getDay()+"-" + userBean.getMonth()+"-"+userBean.getYear());
        uzytkownik.setProperty("email", userBean.getEmail());
        uzytkownik.setProperty("phone", userBean.getPassword());
        try {
			uzytkownik.setProperty("password", Decrypt.getDecrypt(userBean.getPassword()));
		} catch (NoSuchAlgorithmException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        uzytkownik.setProperty("sex", userBean.getSex());
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        datastore.put(uzytkownik);



           
            
            Properties props = new Properties();
            Session session = Session.getDefaultInstance(props, null);

            String msgBody = "To finish" +
                		"the process please enter the following link /n /n /n "+ 
                		"Pozdrawiamy Adminsitracja Serwisu";
            
            try {
                Message msg = new MimeMessage(session);
                msg.setFrom(new InternetAddress("piter.ksc@gmail.com", "Example.com Admin"));
                msg.addRecipient(Message.RecipientType.TO,
                                 new InternetAddress(userBean.getEmail(), userBean.getFirst_name()+" " + userBean.getSecond_name()));
                msg.setSubject("Your Account has been activated");
                msg.setText(msgBody);
                Transport.send(msg);
                log.warning("Wyslano do" +userBean.getEmail());

            } catch (AddressException e) {
            	log.info("chuj");
            } catch (MessagingException e) {
            	log.info("dupa" );
            }
            
            
            response.sendRedirect("Test.jsp");
    					
    		}


}
